package com.fanyouzhao.ehome2.admin.service.impl;

import com.fanyouzhao.ehome2.admin.mapper.AdminMapper;
import com.fanyouzhao.ehome2.admin.service.AdminService;
import com.fanyouzhao.ehome2.admin.service.BaseService;
import com.fanyouzhao.ehome2.core.pojo.Admin;
import com.fanyouzhao.ehome2.core.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: fanyouzhao
 * @date: 14:45 2021/6/22
 */
@Service("adminService")
public class AdminServiceImpl extends BaseServiceImpl<Admin> implements AdminService {

    @Resource
    AdminMapper adminMapper;
    @Resource
    BaseService<Admin> baseService;

    @Override
    public Admin login(Admin admin) throws IllegalAccessException {
        Admin adminJustPhone = new Admin();
        adminJustPhone.setPhone(admin.getPhone());
        List<Admin> adminsBack = baseService.selectByExample(adminJustPhone);
        if (adminsBack!=null&&adminsBack.size()==1){
            System.out.println("adminsBack.get(0) = " + adminsBack.get(0));
            if (admin.getPwd()!=null){
                String finalPwd = MD5Util.encode(MD5Util.encode(admin.getPwd()) + adminsBack.get(0).getSalt());
                if (adminsBack.get(0).getPwd().equals(finalPwd)){
                    return adminsBack.get(0);
                }
            }
        }
        return null;
    }
}
